package com.uestc.servlet.admin.config;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import com.uestc.util.JsonUtil;

import net.sf.json.JSONObject;

public class ReadConfig {
	public static JSONObject jsonObject;
	public static String jsonString;

	static {
		try {
			init();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void init() throws IOException {
		String pathString = ReadConfig.class.getResource("/").getPath();
		pathString = pathString.substring(0, pathString.lastIndexOf("classes")) + "conf/config.json";
		BufferedReader br = new BufferedReader(new FileReader(pathString));
		StringBuilder stringBuilder = new StringBuilder();
		String temp = null;
		while ((temp = br.readLine()) != null) {
			stringBuilder.append(temp);
		}
		jsonString = stringBuilder.toString();
		br.close();
	}
	
	/**
	 * 获取JSON格式的配置文件中的某个属性下的JSON数组
	 * @return
	 */
	public static String[] getItems(String opt) {
		String menus = JsonUtil.getAttribute(jsonString, opt);
		return JsonUtil.getArray(menus);
		
	}
	
	public static String getItem(String opt) {
		return JsonUtil.getAttribute(jsonString, opt);
	}
	
	/**
	 * 返回由Map组成的List
	 * @param opt
	 * @return
	 */
	public static ArrayList<Object> getMap(String opt) {
		String menus = JsonUtil.getAttribute(jsonString, opt);
		return JsonUtil.getMapList(menus);
	}
}
